
#ifndef _PLUGIN_MANAGER_H_
#define _PLUGIN_MANAGER_H_

#include <ace/xcomponent/native_interface_xcomponent.h>

#include "napi/native_api.h"
#include <string>
#include <unordered_map>
//#include "napi/node_api.h"
//#include <uv.h>

#include "plugin_render.h"
#include "message_queue.h"

#define _ARKUI_DECLARATIVE_ 1

// 插件管理器
class PluginManager {
public:
    ~PluginManager() {}

    static PluginManager* GetInstance()
    {
        return &PluginManager::manager_;
    }

    static napi_value GetContext(napi_env env, napi_callback_info info);

    /******************************APP Lifecycle******************************/
    static napi_value NapiOnCreate(napi_env env, napi_callback_info info);
    static napi_value NapiOnShow(napi_env env, napi_callback_info info);
    static napi_value NapiOnHide(napi_env env, napi_callback_info info);
    static napi_value NapiOnDestroy(napi_env env, napi_callback_info info);

//    void OnCreateNative(napi_env env, uv_loop_t* loop);
    void OnShowNative();
    void OnHideNative();
    void OnDestroyNative();
    /*********************************************************************/

#ifdef _ARKUI_DECLARATIVE_
    /******************************声明式范式******************************/
    /**                      JS Page : Lifecycle                        **/
    static napi_value NapiOnPageShow(napi_env env, napi_callback_info info);
    static napi_value NapiOnPageHide(napi_env env, napi_callback_info info);
    void OnPageShowNative();
    void OnPageHideNative();
    /*************************************************************************/

#else
    /******************************类 Web范式******************************/
    /**                      JS Page : Lifecycle                        **/
    static napi_value NapiOnInit(napi_env env, napi_callback_info info);
    static napi_value NapiOnReady(napi_env env, napi_callback_info info);
    static napi_value NapiOnShow(napi_env env, napi_callback_info info);
    static napi_value NapiOnHide(napi_env env, napi_callback_info info);
    static napi_value NapiOnDestroy(napi_env env, napi_callback_info info);
    static napi_value NapiOnActive(napi_env env, napi_callback_info info);
    static napi_value NapiOnInactive(napi_env env, napi_callback_info info);

    void OnInitNative();
    void OnReadyNative();
    void OnShowNative();
    void OnHideNative();
    void OnDestroyNative();
    void OnActiveNative();
    void OnInactiveNative();
    /*************************************************************************/
#endif

    /*************************Worker Func*************************************/
    static napi_value NapiWorkerInit(napi_env env, napi_callback_info info);
    static napi_value NapiASend(napi_env env, napi_callback_info info);

    /*************************************************************************/

    OH_NativeXComponent* GetNativeXComponent(std::string& id);
    void SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent);
    PluginRender* GetRender(std::string& id);

public:
    bool Export(napi_env env, napi_value exports);
    
private:
    static PluginManager manager_;

    std::string id_;
    std::unordered_map<std::string, OH_NativeXComponent*> nativeXComponentMap_;
    std::unordered_map<std::string, PluginRender*> pluginRenderMap_;

public:
    napi_env mainEnv_ = nullptr;
    napi_env workerEnv_ = nullptr;
};

#endif // _PLUGIN_MANAGER_H_